#ifndef IACTION_H
#define IACTION_H
#include <QObject>
#include <QJsonObject>

enum ActionResult
{
    ARSuccess,
    ARCondition, //执行条件不满足
    ARError,     //发生错误，中断不可重试和继续.
};

enum ExecuteRet
{
    ERHold = -2,
    ERError = -1,
    ERSucc = 0,
    ERDefaultSub = 0,
    ERSub1 = 1,
    ERSub2 = 2,
    ERSub3 = 3,
    ERSub4 = 4,
    ERSub5 = 5,
    ERSub6 = 6,
    ERSub7 = 7,
    ERSub8 = 8
};

class IAction : public QObject
{
    Q_OBJECT

public:
    QString id(){return id_;}
    void setId(QString set){id_ = set;}

    void setParam(const QJsonObject &param);
    //把特殊符号参数值替换.比如match="hello",会将${hello}替换为newVal.
    void replaceValue(QString match, QString newVal);

    //把前置动作的结果汇入参数.
    void setPrevResult(const QJsonObject &param){
        //actParam.insert("prev",QJsonValue(param));
        prevResult = param;
    }
    const QJsonObject& getResult(){
        return actResult;
    }
    //把act设为自己的下线，对应位置index
    void setNext(IAction* act,int index=0);
    //把自己设为act的下线，对应index位置.
    void setPrev(IAction* act,int index=0);

    int doExec(); //参见execute的说明.
    QString name(){return classname;}
    QString msg(){return message;}
//signals:
//    void sigExecuteDone(int _ActionResult, IAction* ptr);
    IAction* getNext(int index=0);

    static int freeAllActions();

    static IAction* create(QString name);
    virtual ~IAction();
protected:
    IAction();
    QString classname;
    QString message;
    QJsonObject actParam;  //操作参数.
    QJsonObject actResult; //返回值。覆盖.
    static QJsonObject prevResult; //上一动作的结果

    //使用actParam作为参数执行动作. 可以使用个性函数来配置actParam
    //返回-1失败，0是默认成功，1、2、3分支选择. -2是挂起.
    virtual ExecuteRet execute()=0;
    //下一个action，提供多个出口，默认是0号条件型提供多个出口.
    QList<IAction*> actionNext;

private:
    static QList<IAction*> allActions;
    QString id_;
};

#include "actioncompositekey.h"
#include "actioncondition.h"
//#include "actionfindqqlogin.h"
#include "actionfindwindow.h"
#include "actionhold.h"
#include "actioninput.h"
#include "actionkill.h"
#include "actionmatch.h"
#include "actionmouse.h"
#include "actionprocess.h"
#include "actionqqlogin.h"
#include "actionrelativepos.h"
#include "actionwait.h"
#endif // IACTION_H
